package com.lgl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lgl.constants.SystemConstants;
import com.lgl.dto.AddLinkDto;
import com.lgl.dto.ChangeLinkStatusDto;
import com.lgl.dto.GetLinkListDto;
import com.lgl.dto.UpdateLinkDto;
import com.lgl.entity.Category;
import com.lgl.entity.Link;
import com.lgl.entity.ResponseResult;
import com.lgl.mapper.LinkMapper;
import com.lgl.service.LinkService;
import com.lgl.utils.BeanCopyUtils;
import com.lgl.vo.*;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;


@Service
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements LinkService {

    @Override
    public ResponseResult getAllLink() {
        LambdaQueryWrapper<Link> linkLambdaQueryWrapper = new LambdaQueryWrapper<>();
        linkLambdaQueryWrapper.eq(Link::getStatus, SystemConstants.LINK_STATUS_SUCCESS);
        List<Link> linkList = list(linkLambdaQueryWrapper);
        List<LinkListVo> linkListVos = BeanCopyUtils.copyBeanList(linkList, LinkListVo.class);
        return ResponseResult.okResult(linkListVos);
    }

    @Override
    public ResponseResult getLinkList(Integer pageNum, Integer pageSize, GetLinkListDto getLinkListDto) {
        Page<Link> linkPage = new Page<>();
        linkPage.setSize(pageSize);
        linkPage.setCurrent(pageNum);
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(getLinkListDto.getName()),Link::getName,getLinkListDto.getName());
        queryWrapper.eq(StringUtils.hasText(getLinkListDto.getStatus()),Link::getStatus,getLinkListDto.getStatus());;
        Page<Link> page = page(linkPage, queryWrapper);
        List<Link> records = page.getRecords();
        List<GetLinkListVo> getLinkListVos = BeanCopyUtils.copyBeanList(records, GetLinkListVo.class);
        return ResponseResult.okResult(new PageVo(getLinkListVos,page.getTotal()));
    }

    @Override
    public ResponseResult addLink(AddLinkDto addLinkDto) {
        Link link = BeanCopyUtils.beanCopy(addLinkDto, Link.class);
        save(link);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getLink(Long id) {
        Link link = getById(id);
        GetLinkVo getLinkVo = BeanCopyUtils.beanCopy(link, GetLinkVo.class);
        return ResponseResult.okResult(getLinkVo);
    }

    @Override
    public ResponseResult updateLink(UpdateLinkDto updateLinkDto) {
        Link link = BeanCopyUtils.beanCopy(updateLinkDto, Link.class);
        updateById(link);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteLink(Long id) {
        removeById(id);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult changeLinkStatus(ChangeLinkStatusDto changeLinkStatusDto) {
        getBaseMapper().changeLinkStatus(changeLinkStatusDto);

        return ResponseResult.okResult();
    }
}

